using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.ComponentModel;

namespace LogFileViewer
{
    class LogfileIndexer : LogfileAccess
    {
        public LogfileIndexer(FileStream str) : base(str)
        {
        }

        public void countLinesOnly(DataAccess dataAccess)
        {
            this.startProcess("Counting log entries...");

            Regex regex = new Regex(RegExProvider.LOG_STRUCTURE_01, RegexOptions.Multiline);

            string tempLine;
            this.fileStream.Position = 0;
            do
            {
                tempLine = this.streamReader.ReadLine();

                if (tempLine != null && regex.IsMatch(tempLine))
                {
                    // Count existing log entries in file
                    dataAccess.EveryLogEntriesAmountAll++;
                }

                this.incrementProgressBar();

            } while (tempLine != null);

            this.stopProcess();
        }

        public Dictionary<MessageTypes, long[]> readLineIndexes(DataAccess dataAccess)
        {

            return null;

            //string description = "Reading line indexes...";
            //this.startProcess(description);
            //Console.Write(description);
            //DateTime startTime = DateTime.Now;

            //int maxProgressBar = 1000;
            //int pieceSize = (int)(this.fileStream.Length / maxProgressBar);

            //ProgressWindow progressWindow = new ProgressWindow();
            //progressWindow.ChangeProgressDescription(description);
            //progressWindow.Show();

            //ArrayList[] lists = new ArrayList[MessageType.Length()];

            //string tempLine;
            //long lineStart = 0;
            //this.fileStream.Position = lineStart;
            //long piecesDone = 0;
            //do
            //{
            //    StringBuilder cache = new StringBuilder();
            //    int found = -1;
            //    int size = 256;
            //    while (found == -1)
            //    {
            //        byte[] bytes = new byte[size];
            //        this.fileStream.Read(bytes, 0, size);
            //        // Look for the next linebreak after search position
            //        string s = Encoding.Default.GetString(bytes);
            //        cache.Append(s);
            //        found = s.IndexOf("\r\n");
            //        if ((cache.Length + size) > this.fileStream.Length)
            //        {
            //            break;
            //        }
            //    }

            //    if (found == -1)
            //    {
            //        tempLine = null;
            //    }
            //    else
            //    {
            //        found += cache.ToString().Length - size;
            //        tempLine = cache.ToString().Substring(0, found);
            //    }

            //    this.incrementProgressBar();

            //    if (tempLine == null)
            //    {
            //        continue;
            //    }

            //    // Find out what message type
            //    if (tempLine.IndexOf(".") == 2 && tempLine.IndexOf(" ") == 10)
            //    {
            //        int start = tempLine.IndexOf('[');
            //        int end = tempLine.IndexOf(']');
            //        if (start == -1 || end == -1)
            //        {
            //            continue;
            //        }
            //        string type = tempLine.Substring(start + 1, end - start - 1);
            //        if (type == null)
            //        {
            //            continue;
            //        }
            //        for (int i = 0; i < MessageType.Length(); i++)
            //        {
            //            if (type.Equals(MessageType.StringArray()[i]))
            //            {
            //                if (lists[i] == null)
            //                {
            //                    lists[i] = new ArrayList();
            //                }

            //                // Add current log entry line number to list
            //                lists[i].Add(lineStart);

            //                // Additionally count existing log entries in file
            //                MessageTypes currentType = MessageType.MessageTypeAt(i);
            //                if (dataAccess.EveryLogEntriesAmount == null)
            //                {
            //                    dataAccess.EveryLogEntriesAmount = new Dictionary<MessageTypes, int>();
            //                }
            //                if (!dataAccess.EveryLogEntriesAmount.ContainsKey(currentType))
            //                {
            //                    dataAccess.EveryLogEntriesAmount.Add(currentType, 0);
            //                }
            //                dataAccess.EveryLogEntriesAmount[currentType]++;

            //                break;
            //            }
            //        }

            //    }

            //    // End of file is reached
            //    if (found == -1 && fileStream.Length < (fileStream.Position + size))
            //    {
            //        break;
            //    }

            //    // Set next line start delimiter
            //    lineStart += found + 2;
            //    fileStream.Position = lineStart;

            //    if (this.fileStream.Position > piecesDone)
            //    {
            //        progressWindow.SetProgressBarValue(1);
            //        Console.WriteLine(">> " + this.fileStream.Position);
            //        piecesDone += pieceSize;
            //    }

            //} while (tempLine != null);

            //// Save arrays in variable
            //Dictionary<MessageTypes, long[]> indexes = new Dictionary<MessageTypes, long[]>(MessageType.Length());
            //for (int i = 0; i < MessageType.Length(); i++)
            //{
            //    ArrayList list = lists[i];
            //    if (list != null)
            //    {
            //        long[] tempArray = (long[])list.ToArray(typeof(long));
            //        MessageTypes type = MessageType.MessageTypeAt(i);
            //        if (!indexes.ContainsKey(type))
            //        {
            //            indexes.Add(MessageType.MessageTypeAt(i), tempArray);
            //        }
            //        else
            //        {
            //            indexes[MessageType.MessageTypeAt(i)] = tempArray;
            //        }
            //    }
            //}

            //this.stopProcess();

            //progressWindow.Close();

            //DateTime stopTime = DateTime.Now;
            //TimeSpan timespan = new TimeSpan(stopTime.Ticks - startTime.Ticks);
            //Console.WriteLine(timespan.ToString() + " ms");
            

            //return indexes;
        }
    }
}
